﻿using mozhi.smarterp.Finance.Basedatas.Events;
using System;
using System.Collections.Generic;
using Volo.Abp;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace mozhi.smarterp.Finance.Banks
{
    public class BankAccount : AggregateRootBase<Guid>
    {
        /// <summary>
        /// 银行号码
        /// </summary>
        public string Number { private set; get; }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// 币别
        /// </summary>
        public Guid CurrencyId { get; private set; }

        /// <summary>
        /// 银行类型
        /// </summary>
        public Guid? BankTypeId { get; private set; }

       

        /// <summary>
        /// 开启勾兑
        /// </summary>
        public bool EnableReconcile { set; private get; }

        /// <summary>
        /// 开户信息
        /// </summary>
        public string BranchInfo { get; private set; }

        /// <summary>
        /// 入账科目
        /// </summary>
        public Guid? AccountId { get; private set; }

        private BankAccount() { }

        public BankAccount(Guid id, string name, Guid currencyId, Guid? bankTypeId, string number, bool enableReconcile, string brachInfo)
        {
            Id = id;
            Name = name;
            CurrencyId = currencyId;
            BankTypeId = bankTypeId;
            Number = number;
            EnableReconcile = enableReconcile;
            BranchInfo = brachInfo;
            Activated = true;

            Validate();

            if (bankTypeId.HasValue)
                AddDistributedEvent(new DataAssociationAddEvent(Id, nameof(BankAccount), "银行账号", "", Name, bankTypeId.Value, "BankType"));

            AddDistributedEvent(new DataAssociationAddEvent(Id, nameof(BankAccount), "银行账号", "", Name, currencyId, "Currency"));
        }

        public void Change(string name, Guid currencyId, Guid? bankTypeId, string number, bool enableReconcile, string brachInfo)
        {

            Name = name;
            CurrencyId = currencyId;
            BankTypeId = bankTypeId;
            Number = number;
            EnableReconcile = enableReconcile;
            BranchInfo = brachInfo;

            Validate();
        }

        public void SetAccount(Guid accountId)
        {
            AccountId = accountId;
        }

        #region
        private void Validate()
        {
            if (Name.IsNullOrEmpty())
                throw new UserFriendlyException("名称不能为空", "100000");

            if (CurrencyId == default)
                throw new UserFriendlyException("币别不能为空", "100000");

            if (!BankTypeId.HasValue)
                throw new UserFriendlyException("银行类型不能为空", "100000");
        }
        #endregion

    }
}
